package com.efast.dmom.mes.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mes.bean.EquipRepairInfo;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.EquipRepairInfoDao;
import com.efast.dmom.mes.entity.EquipRepairInfoEntity;
import com.efast.dmom.mes.service.EquipRepairInfoService;
import com.efast.dmom.mes.service.EquipRepairSpareInfoService;
import com.efast.dmom.mes.util.ServiceBaseEntityUtil;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;
import com.efast.dmom.mes.util.WmsOrderStatusUtil;

@Service
public class EquipRepairInfoServiceImpl implements EquipRepairInfoService{
	
    @Autowired
    private EquipRepairInfoDao equipRepairInfoDao;
    
	@Autowired
	private ServiceBaseEntityUtil serviceBaseEntityUtil;
	
	@Autowired
	private EquipRepairSpareInfoService equipRepairSpareInfoService;
	
	
	@Override
	public ResultBean save(EquipRepairInfo equipRepairInfo) {
		ResultBean resultBean = new ResultBean(true,' ');
		EquipRepairInfoEntity repairInfoEntity = equipRepairInfo.getInnerData();
		repairInfoEntity.setRepairInfoNumber(serviceBaseEntityUtil.getNo(WmsOrderStatusUtil.REPAIRINFOCODE));
		repairInfoEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
		repairInfoEntity.setCreationTime(TimeUtil.getLocalDateTime());
		repairInfoEntity.setCreator(SpringWebUtils.getRequestUser());
		repairInfoEntity.setRowStatus(0);
		equipRepairInfoDao.save(repairInfoEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("保存成功！");
		return resultBean;
	}

	@Override
	public ResultBean update(EquipRepairInfo equipRepairInfo) {
		ResultBean resultBean = new ResultBean(true,' ');
		EquipRepairInfoEntity repairInfoEntity = equipRepairInfo.getInnerData();
		EquipRepairInfoEntity EquipRepairInfoEntity = equipRepairInfoDao.getById(repairInfoEntity.getId());
		if (EquipRepairInfoEntity.getEquipId() != repairInfoEntity.getEquipId()) {
			resultBean.setSuccess(false);
			resultBean.setMessage("维修设备不可修改！");
			return resultBean;
		}
		repairInfoEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		repairInfoEntity.setLastModifier(SpringWebUtils.getRequestUser());
		equipRepairInfoDao.update(repairInfoEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("更新成功！");
		return resultBean;
	}

	@Override
	public ResultBean delete(EquipRepairInfo equipRepairInfo) {
		ResultBean resultBean = new ResultBean(true,' ');
		EquipRepairInfoEntity repairInfoEntity = equipRepairInfo.getInnerData();
		repairInfoEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		repairInfoEntity.setLastModifier(SpringWebUtils.getRequestUser());
		repairInfoEntity.setRowStatus(1);
		equipRepairInfoDao.delete(repairInfoEntity);
		equipRepairSpareInfoService.deleteByRepairInfoId(repairInfoEntity.getId());
		resultBean.setSuccess(true);
		resultBean.setMessage("删除成功！");
		return resultBean;
	}

}
